function multiPageTable(input, options)
% Function to convert a three-dimensional matrix into a set of Tex/PDF
% tables.
% =========================================================================================
% [Inputs]:
% input.dataMat: a 2 or 3 diml matrix, each sheet of which is a table
% input.colLabels: cell of column labels which will be applied to all tables
% input.rowLabels: cell of row labels which will be applied to all tables
% input.tableHeader: cell of titles to use for each table
% input.corner: string to put in upper left hand corner of table
% 
% [Options]: (indeed optional)
% options.fileName: optional name for the file name
% options.printPDF: logical, 1 to print PDF
% options.outPath: optional folder to place created tables
% options.orientation: 'portrait' or 'landscape'
% options.maxRows: maximum number of rows per page
% options.maxCols: maximum number of columns per page
% *option.fileExists*
% Modifications 
% AJ Jan 4 2012. Added *options.fileExists* if true or 1, then the file is
% assumed to have been opened earlier and has name options.filename and options.file Default is zero 

% =========================================================================================

% Extract inputs for table
mat = input.dataMat;
colLabels = input.colLabels;
rowLabels = input.rowLabels;
tableHeader = input.tableHeader;

% Code assumed talbeHeader is a cell
if ischar(tableHeader)
    tableHeader = {tableHeader};
end

if nargin < 2
    options = [];
end

if ~isfield(options,'orientation')
     options.orientation = 'portrait';
end

%% Determine if need to open a file or file already exists 
if ~isfield(options,'fileExists'); 
    options.fileExists=false; 
else 
    if options.fileExists==1 
        options.fileExists=true; 
    end 
    if options.fileExists~=1 
        options.fileExists=false; 
    end 
end 
if options.fileExists==false 
    disp('Opening New File') 
else 
    disp('Writting on Existing File') 
end 
% If the maximum number of rows unspceified, set to default for each
% possible orientation
if ~isfield(options,'maxRows')
    if strcmp(options.orientation,'landscape')
       options.maxRows = 10;
    elseif strcmp(options.orientation,'portrait')
       options.maxRows = 20;
    else
        error('Orientation must be either "portrait" or "landscape"')
    end
end

% Same for cols
if ~isfield(options,'maxCols')
    if strcmp(options.orientation,'landscape')
        options.maxCols = 8;
    elseif strcmp(options.orientation,'portrait')
        options.maxCols = 5;
    else
        error('Orientation must be either "portrait" or "landscape"')
    end
end

if ~isfield(input,'corner')
    input.corner = '';
end

nTables = size( mat,3 );

% Given the maximum number of rows and columns, determine how many pages
% will be needed to print one table
rowParts = ceil( size(mat,1)/options.maxRows ); 
colParts = ceil( size(mat,2)/options.maxCols );
maxRows  = options.maxRows;
maxCols  = options.maxCols;
table.corner = input.corner;

%% Print the Tables

% There are two cases to consider: printing only one page v. multiple pages
if (nTables == 1) && (rowParts == 1) && (colParts == 1) % If we are only print one page total
    
    % Set table data/labels
    table.dataMat = mat;
    table.colLabels = colLabels;
    table.rowLabels = rowLabels;
    table.tableName = tableHeader;
    
    % Set printing options
    if options.fileExists==false;
        options.open=true;
        options.append = false;
    else
        options.open=false;
        options.append=true; 
    end
    options.close=false;
    
    % Print table
    makeLatexTableCont(table,options);

else % If there are many pages to print
    for p = 1:nTables
    
        for i = 1:rowParts
        
            for j =1:colParts
                
                % Extract the indices of rows and columns to print
                rowInx = (maxRows*(i-1) + 1):min(maxRows*i, size(mat,1));
                colInx = (maxCols*(j-1) + 1):min(maxCols*j, size(mat,2));
                
                table.dataMat = mat(rowInx,colInx,p);
                table.rowLabels = rowLabels(rowInx);
                table.colLabels = colLabels(colInx);
                
                % If a table needs to be broken up into smaller parts,
                % modify the table header to indicate this
                if (rowParts == 1) && (colParts == 1)
                    table.tableHeader = tableHeader{p};
                else 
                    table.tableHeader = [tableHeader{p},' Part ',num2str((i-1)*rowParts+j),...
                                       ' of ',num2str(colParts*rowParts)];
                end
                
                if (i == 1)&&(j == 1)&&(p == 1) % For printing the first page, open file
                    if options.fileExists==false;
                        options.open=true;
                        options.append = false;
                    else
                        options.open=false;
                        options.append =true;
                    end                                        
                    options.close = false;                    
                elseif (i == rowParts)&&(j == colParts)&&(p == nTables) % For closing file
                    
                    options.open = false;
                    options.append = true;
                    options.close = true;
                    
                else % For appending intermediate pages
                    
                    options.open = false;
                    options.append = true;
                    options.close = false;
                    
                end
                
                makeLatexTableCont(table,options);

            end
        end
    end
end